package com.example.liwushuo.fragment;

import java.util.ArrayList;
import java.util.List;

import com.example.liwushuo.BaseApp;
import com.example.liwushuo.R;
import com.example.liwushuo.adapter.GoodsInfoAdapter;
import com.example.liwushuo.adapter.TallAdapter;
import com.example.liwushuo.bar.GoodsInfoBar;
import com.example.liwushuo.bean.Comment;
import com.example.liwushuo.bean.Item;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.viewpagerindicator.TabPageIndicator;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GoodsInfoFragment extends Fragment {

	private ViewPager viewPager;
	private TextView name;
	private TextView price;
	private TextView info;
	private Item item;

	// viewpager的适配器
	private GoodsInfoAdapter adapter;
	private List<ImageView> list;

	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		item = (Item) getArguments().getSerializable("item");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_goodsinfo, null);
		
		GoodsInfoBar goodsInfoBar = new GoodsInfoBar();
		Bundle args = new Bundle();
		args.putSerializable("item", item);
		goodsInfoBar.setArguments(args);

		getFragmentManager().beginTransaction()
				.replace(R.id.goodsinfo_bar, goodsInfoBar).commit();
		initView(view);
		initData();
		loadData();
		return view;
	}

	private void initView(View view) {
		name = (TextView) view.findViewById(R.id.GoodsInfo_name);
		price = (TextView) view.findViewById(R.id.GoodsInfo_price);
		info = (TextView) view.findViewById(R.id.GoodsInfo_info);
		viewPager = (ViewPager) view.findViewById(R.id.goodsinfo_viewPager);
		name.setText(item.getData().getName());
		price.setText(item.getData().getPrice());
		list = new ArrayList<ImageView>();
		adapter = new GoodsInfoAdapter(list);

	}

	private void initData() {
		name.setText(item.getData().getName());
		price.setText(item.getData().getPrice());
		info.setText(item.getData().getDescription());
		viewPager.setAdapter(adapter);

	}

	private void loadData() {
		ImageView img = null;

		for (int i = 0; i < item.getData().getImage_urls().size(); i++) {
			img = new ImageView(getActivity());
			ViewGroup.LayoutParams pamams = new ViewGroup.LayoutParams(
					ViewGroup.LayoutParams.MATCH_PARENT,
					ViewGroup.LayoutParams.MATCH_PARENT);
			img.setLayoutParams(pamams);
			img.setScaleType(ImageView.ScaleType.FIT_XY);
			ImageLoader.getInstance().displayImage(
					(String) item.getData().getImage_urls().get(i), img);
			list.add(img);
		}
		adapter.notifyDataSetChanged();
	}
}
